package session.stateless;

import entity.CRM.ProductPrice;
import entity.Product;
import javaclass.ProductType;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class ProductBean implements ProductBeanLocal {
    @PersistenceContext()
    EntityManager em;
    boolean success = false;
    
    public ProductPrice createProductPrice(ProductType productType, Long lowerLimit, Long higherLimit, Double price) {
        ProductPrice productPrice = new ProductPrice();
        productPrice.setProductType(productType);
        productPrice.setLowerLimit(lowerLimit);
        productPrice.setHigherLimit(higherLimit);
        productPrice.setProductPrice(price);
        em.persist(productPrice);
        em.flush();
        return productPrice;
    }

    public void updateProductPrice(Long productPriceId, ProductType productType, Long lowerLimit, Long higherLimit, Double price) {
        ProductPrice productPrice = em.find(ProductPrice.class, productPriceId);
        
        if(productPrice==null){
            System.out.println("This productPrice type does not exist!");
        }
        else{
            productPrice.setLowerLimit(lowerLimit);
            productPrice.setHigherLimit(higherLimit);
            productPrice.setProductPrice(price);
            em.flush();
         }
    }

    public void deleteProductPrice(Long productPriceId) {
        ProductPrice productPrice = em.find(ProductPrice.class, productPriceId);
        
        if(productPrice==null){
            System.out.println("This productPrice type does not exist!");
        }
        else{
            em.remove(productPrice);
            }
        }
    
    public ProductPrice lookupRange(ProductType productType, Long quantity)
    {
        Query q = em.createQuery("SELECT c FROM ProductPrice c WHERE c.productType=:productType");
        q.setParameter("productType", productType);
        List<ProductPrice> productPrices = q.getResultList();
        
        for (ProductPrice p: productPrices)
        {
            if ((quantity >=p.getLowerLimit()) && (p.getHigherLimit() > quantity))            
                return p;            
        }
        return null;
    }
    
    public List<Product> getProductList() {
        Query q = em.createQuery("SELECT c FROM ProductPrice c");
        return q.getResultList();
    }
    
    public boolean checkConflict(Long productPriceId, ProductType productType, Long lowerLimit, Long higherLimit) {
        Query q=em.createQuery("SELECT c FROM ProductPrice c WHERE c.productType =:productType");
        q.setParameter("productType", productType);
        List<ProductPrice> temp =q.getResultList();
        
        for (ProductPrice productPrice:temp)
        {
            if ((productPrice.getHigherLimit()>lowerLimit) & (productPrice.getLowerLimit()<higherLimit)) 
            {
                    if ((productPriceId == -1) || (productPrice.getProductPriceId()!=productPriceId))
                    return true;
            }
        }
        return false;
    }

        public List<ProductPrice> getProductPricesByType(ProductType productType) {
        Query q=em.createQuery("SELECT c FROM ProductPrice c WHERE c.productType=:productType");
        q.setParameter("productType", productType);
        return q.getResultList();
    }

    public ProductPrice getFirstProductPriceA() throws NonUniqueResultException {
        Query q=em.createQuery("SELECT c FROM ProductPrice c WHERE c.productType =:productType");
        q.setParameter("productType",ProductType.NUTS);
        return (ProductPrice) q.getResultList().get(0);        
    }

    @Override
    public ProductPrice getFirstProductPriceB() throws NonUniqueResultException{
        Query q=em.createQuery("SELECT c FROM ProductPrice c WHERE c.productType =:productType");
        q.setParameter("productType",ProductType.FRUITS);
        return (ProductPrice) q.getResultList().get(0);   
    }

    @Override
    public ProductPrice getFirstProductPriceC() throws NonUniqueResultException{
        Query q=em.createQuery("SELECT c FROM ProductPrice c WHERE c.productType =:productType");
        q.setParameter("productType",ProductType.CHOCOLATE_NUTS);
        return (ProductPrice) q.getResultList().get(0);
    }

    @Override
    public ProductPrice getFirstProductPriceD() throws NonUniqueResultException{
        Query q=em.createQuery("SELECT c FROM ProductPrice c WHERE c.productType =:productType");
        q.setParameter("productType",ProductType.REDCURRANT_FRUITS);
        return (ProductPrice) q.getResultList().get(0);
    }

 
}
